package com.leo;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.leo.base.init.CustomSplash;
import com.leo.view.LoginView;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import de.felixroske.jfxsupport.GUIState;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.scene.image.Image;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ImportResource;

import java.net.URL;

/**
 * @author wangss
 */
@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})
@ImportResource("classpath:spring/applicationContext.xml")
@Slf4j
public class LeoSpringbootJavafxApplication extends AbstractJavaFxApplicationSupport {

    public static void main(String[] args) {
        launch(LeoSpringbootJavafxApplication.class, LoginView.class, new CustomSplash(), args);
    }

    @Override
    public void start(Stage stage) throws Exception {
        // 设置初始化界面不能更改
        stage.setResizable(false);
        // 在屏幕中央
        stage.centerOnScreen();
        super.start(stage);
    }

    @Override
    public void beforeInitialView(Stage stage, ConfigurableApplicationContext ctx) {
        // 设置模式为透明模式
        stage.initStyle(StageStyle.TRANSPARENT);
        // 当stage为新的展示状态时，设置scene为透明背景，加载css样式文件
        stage.showingProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
                if (null != newValue && newValue && GUIState.getScene().getStylesheets().size() == 0) {
                    // jfoenix 样式
                    URL mainCss = this.getClass().getResource("/static/css/jfoenix-main.css");
                    URL componentsCss = this.getClass().getResource("/static/css/jfoenix-components.css");
                    // 自定义样式
                    URL mystyleCss = this.getClass().getResource("/static/css/mystyle.css");
                    // BootstrapFX 样式
                    URL bootstrapFXCss = this.getClass().getResource("/org/kordamp/bootstrapfx/bootstrapfx.css");
                    GUIState.getScene().getStylesheets().addAll(mainCss.toExternalForm(),
                            componentsCss.toExternalForm(), mystyleCss.toExternalForm(), bootstrapFXCss.toExternalForm());
                    GUIState.getScene().setFill(Paint.valueOf("#ffffff00"));
                }
            }
        });
    }

    /*@Override
    public void beforeShowingSplash(Stage splashStage) {
        if (splashStage.getIcons().size() > 0) {
            ObservableList<Image> icons = splashStage.getIcons();
            icons.removeAll(icons);
            Image image = new Image(this.getClass().getResource("/static/icon/icon.png").toExternalForm());
            splashStage.getIcons().addAll(image);
        }
    }*/
}
